package com.dodock.android.banglapapers.ui;

import java.util.Vector;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dodock.android.banglapapers.BanglaNews;
import com.dodock.android.banglapapers.Category;
import com.dodock.android.banglapapers.ComplexScriptHelper;
import com.dodock.android.banglapapers.MyApplication;
import com.dodock.android.banglapapers.R;
import com.dodock.android.banglapapers.imageLoader.ImageLoader;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

public class FavouritNewsActivity extends BaseActivity{
	private Typeface fontBN;
	private Typeface fontEN;
	private Vector<Category> categoryList;
	
	private Tracker mGaTracker;
	private GoogleAnalytics mGaInstance;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Google Analytics
		try{
		    mGaInstance = GoogleAnalytics.getInstance(this);
		    mGaTracker = mGaInstance.getTracker(getResources().getString(R.string.ga_trackingId));
		}
		catch (Exception e) {
		}
		//--Google Analytics
		
		
		setContentView(R.layout.home);
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText(R.string.favorite_category);
		
		ImageButton settingsButton = (ImageButton) findViewById(R.id.settingsBtn);
		settingsButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				settingsActivity();
			}
		});
		
		fontBN = Typeface.createFromAsset(this.getAssets(), "font/SutonnyMJ.ttf");
		fontEN = Typeface.DEFAULT;
		Button topNewsButton = (Button) findViewById(R.id.topNews);
		topNewsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FavouritNewsActivity.this, TopNewsActivity.class);
				startActivity(intent);
			}
		});

		Button favouritButton = (Button) findViewById(R.id.favorite);
		favouritButton.setVisibility(View.INVISIBLE);

		Button savedNewsButton = (Button) findViewById(R.id.savedNews);
		savedNewsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FavouritNewsActivity.this, SavedNewsActivity.class);
				startActivity(intent);
			}
		});
		
		
		setInContentView();
	}
	
	private void setInContentView() {
		// TODO Auto-generated method stub
		LinearLayout mainContainer = (LinearLayout) findViewById(R.id.homeMainContainer);
		if(HomeActivity.newspapersList == null || HomeActivity.newspapersList.size() < 1){
			return;
		}
		
		for(int i = 0; i < HomeActivity.newspapersList.size(); i++){
			final int paperno = i;
			categoryList = BanglaNews.getDatabaseManager().getCategoriesOfPaper(HomeActivity.newspapersList.elementAt(paperno).getId());
			
			if(categoryList.size() > 0 ){
				LinearLayout container = new LinearLayout(this);
				LinearLayout.LayoutParams containerParms = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT, 70);
				containerParms.setMargins(0, 5, 0, 0);
				container.setLayoutParams(containerParms);
				container.setBackgroundColor(Color.WHITE);
				container.setGravity(Gravity.CENTER);
				ImageLoader imageLoader = new ImageLoader(this);
				ImageView logo = new ImageView(this);
				LinearLayout.LayoutParams logoParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
				logo.setLayoutParams(logoParams);
				imageLoader.DisplayImage(BanglaNews.BASE_URL + HomeActivity.newspapersList.get(i).getLogo(),
						R.drawable.logo, logo, true, container);
				
				mainContainer.addView(container);
				for(int j = 0; j < categoryList.size(); j++){
					
					LinearLayout newsCategoryLayout = new LinearLayout(this);
					newsCategoryLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
					LinearLayout.LayoutParams categoryParms = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, 60);
					if(j == categoryList.size()-1){
						categoryParms.setMargins(0, 0, 0, 15);
					}
					newsCategoryLayout.setLayoutParams(categoryParms);
					TextView categoryName = new TextView(this);
					final int position = j;
					categoryName.setOnClickListener(new OnClickListener() {
						String key = categoryList.elementAt(position).getKey();
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(FavouritNewsActivity.this, NewsListActivity.class);
							intent.putExtra("id", HomeActivity.newspapersList.get(paperno).getId());
							intent.putExtra("language", HomeActivity.newspapersList.get(paperno).getLanguage());
							intent.putExtra("key", key);
							intent.putExtra("paperName", HomeActivity.newspapersList.get(paperno).getName());
							startActivity(intent);
						}
					});
					categoryName.setTextAppearance(getBaseContext(), android.R.style.TextAppearance_Large);
					LinearLayout.LayoutParams textViewParms = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, 60);
					textViewParms.gravity = Gravity.CENTER_VERTICAL;
					textViewParms.setMargins(15, 0, 15, 0);
					categoryName.setLayoutParams(textViewParms);
					categoryName.setTextColor(Color.BLACK);
					categoryName.setGravity(Gravity.CENTER_VERTICAL);
					categoryName.setPadding(15, 0,15,0);
					
					if(categoryList.size() == 1){
						categoryName.setBackgroundResource(R.drawable.round_rectangle_white);
					}else{
						if(j == 0){
							categoryName.setBackgroundResource(R.drawable.roundtopgray);
						}else if( j == categoryList.size() - 1){
							categoryName.setBackgroundResource(R.drawable.roundbottomgray);
						}else{
							categoryName.setBackgroundResource(R.drawable.rectanglegray);
						}
					}
					if(HomeActivity.newspapersList.get(i).getLanguage().equals("bn")){
						categoryName.setTypeface(fontBN);
						categoryName.setText(ComplexScriptHelper.UnicodeToAnsi(categoryList.elementAt(j).getDisplay()));
					}else{
						categoryName.setTypeface(fontEN);
						categoryName.setText(categoryList.elementAt(j).getDisplay());
					}
					newsCategoryLayout.addView(categoryName);
					mainContainer.addView(newsCategoryLayout);
				}
			}
		}
		
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		com.facebook.Settings.publishInstallAsync(this, MyApplication.fb_app_id);
	}
	

	@Override
	protected void onStart() {
		super.onStart();
		try {
			mGaTracker.sendView("/Favorites");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
}
